package net.lab1024.sa.admin.module.business.supercomputer.datasource.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.lab1024.sa.admin.module.business.supercomputer.datasource.domain.form.DatasourceAddForm;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@RestController
@Tag(name = "数据源管理")
public class ConnectController {
    @Operation(summary = "MySQL数据库连接 @author shyu")
    @PostMapping("/datasource/mysqlconnect")
    //@SaCheckPermission("datasource:query")
    public ResponseDTO<String> MySqLConnect(@RequestBody @Valid DatasourceAddForm addForm) throws ClassNotFoundException {
        System.out.println(addForm);
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(addForm.getConnInformation(), JsonObject.class);
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = jsonObject.get("url").getAsString();
        System.out.println(url);
        String user = jsonObject.get("username").getAsString();;
        System.out.println(user);
        String password = jsonObject.get("password").getAsString();;
        System.out.println(password);

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, password);
            System.out.println("Connected to the database successfully!");
        } catch (SQLException e) {
            System.out.println("Failed to connect to the database.");
            e.printStackTrace();
        }
        return ResponseDTO.ok();
    }
    @Operation(summary = "MySQL数据库连接测试 @author shyu")
    @PostMapping("/datasource/mysqlconnecttest")
    //@SaCheckPermission("datasource:query")
    public ResponseDTO<String> MySqLConnectTest(@RequestBody @Valid DatasourceAddForm addForm) throws ClassNotFoundException, SQLException {
        //System.out.println(addForm);
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(addForm.getConnInformation(), JsonObject.class);
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = jsonObject.get("url").getAsString();
        System.out.println(url);
        String user = jsonObject.get("username").getAsString();;
        System.out.println(user);
        String password = jsonObject.get("password").getAsString();;
        System.out.println(password);

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, password);
            System.out.println("Connected to the database successfully!");
        } catch (SQLException e) {
            System.out.println("Failed to connect to the database.");
            e.printStackTrace();
        }
        connection.close();
        return ResponseDTO.ok();
    }
}
